<template>
  <div>
    <el-row>
      <el-dialog v-dialogDrag :visible.sync="visible" :title="title" center :width="width">
        <el-form
          :model="form"
          size="mini"
          :rules="rules"
          ref="ruleForm"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="userName">
            <el-input v-model="form.userName" :placeholder="rules.userName[0].message">
            </el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="relName">
            <el-input v-model="form.relName" :placeholder="rules.relName[0].message"></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-input v-model="form.avatar"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="form.phone" :placeholder="rules.phone[0].message"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" :placeholder="rules.email[0].message"></el-input>
          </el-form-item>
<!--          <div v-if="true">-->
<!--            <el-form-item label="注册时间">-->
<!--              <el-input :value="form.regTime"></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="介绍">-->
<!--              <el-input :value="form.introduction"></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="在线时长">-->
<!--              <el-input :value="form.onlineTime"></el-input>-->
<!--            </el-form-item>-->
<!--          </div>-->
        </el-form>
        <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="visible = false">取 消</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
          <el-button size="mini" @click="save" type="primary">保 存</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
  import {validPhone, validEmail} from '@/utils/validate'
  import {USER} from "@/api"

  export default {
    components: {},
    props: {
      show: {Boolean, default: false},
      value: {Object, default: {}},
      width: {String, default: "50%"},
      title: {String, default: "title"},
      type: {Number, default: 0} // 0 新增 1 编辑 2 查看
    },
    data() {
      return {
        form: {},
        rules: {
          userName: [{required: true, message: "请输账号", trigger: "blur"}],
          relName: [
            {required: false, message: "请输入昵称", trigger: "blur"}
          ],
          phone: [{required: true,validator: validPhone, message: "请输入电话", trigger: "blur"}
          ],
          email: [
            {
              required: true, validator: (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('请输入邮箱'))
                } else if (validEmail(value)) {
                  callback()
                } else {
                  callback(new Error('非法邮箱'))
                }
              }, message: "请输入邮箱", trigger: "blur"
            }
          ]
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.show
        },
        set(val) {
          this.$emit("update:show", false)
        }
      }
    },
    mounted() {
      this.setOldData(this.value)
    },
    watch: {},
    methods: {
      setOldData(data) {
        this.form = this.$newObject(data)
      },
      save() {
        if (this.$validate(this, "ruleForm")) {
          USER.update(this.form).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
            this.$emit("update:show", false)
            this.$emit('refresh')
          })
        }
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
        this.setOldData(this.value)
      }
    }
  }
</script>
<style lang="scss" scoped>
  .small-card {
    ::v-deep .el-card__body {
      padding: 10px;
    }
  }

  .el-form {
    ::v-deep .el-form-item {
      margin-bottom: 10px;
    }
  }
</style>
